﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Mvc;
using Proggy.Web.Cms.Repositories.Membership;
using Proggy.Web.Cms.ViewModels;
using Proggy.Web.Cms.Membership;
using Proggy.Web.Cms.Context;

namespace Proggy.Web.Cms.Controllers.BackEnd
{
    /// <summary>
    /// Membership controller
    /// </summary>
    public class MembershipAndGroupsController : BackEndControllerBase
    {
        /// <summary>
        /// Constructor which sets membership repository
        /// </summary>
        /// <param name="membershipRepository"></param>
        public MembershipAndGroupsController(IMembershipRepository membershipRepository)
        {
            this.MembershipRepository = membershipRepository;
        }

        /// <summary>
        /// Membership repository
        /// </summary>
        public IMembershipRepository MembershipRepository { get; set; }

        /// <summary>
        /// Index of members
        /// </summary>
        /// <returns></returns>
        public ActionResult Index()
        {
            //
            throw new Exception("CHANGE THIS TO A GENERIC HOME PAGE");
        }

        /// <summary>
        /// Change password
        /// </summary>
        /// <returns></returns>
        public ActionResult ChangePassword()
        {

            //
            return View();
        }

        /// <summary>
        /// New member
        /// </summary>
        /// <returns></returns>
        [ProggyBackEndAuthorize(MembershipConstants.MEMBERSHIP_MANAGER_GROUP_NAME)]
        public ActionResult MemberNew()
        {
            //
            return View();
        }

        /// <summary>
        /// Groups GET
        /// </summary>
        /// <returns></returns>
        [ProggyBackEndAuthorize(MembershipConstants.MEMBERSHIP_MANAGER_GROUP_NAME)]
        public ActionResult Groups()
        {
            // get groups
            var groups = this.MembershipRepository.GetGroups();

            //
            return View(groups.AsEnumerable());
        }

        /// <summary>
        /// Edit group GET
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [ProggyBackEndAuthorize(MembershipConstants.MEMBERSHIP_MANAGER_GROUP_NAME)]
        public ActionResult EditGroup(int id)
        {
            // get model
            var group = this.MembershipRepository.GetGroupViewModel(id);

            // 
            return View(group);
        }

        /// <summary>
        /// Add group member GET
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [ProggyBackEndAuthorize(MembershipConstants.MEMBERSHIP_MANAGER_GROUP_NAME)]
        [HttpGet]
        public ActionResult AddGroupMembers(int id)
        {
            // get view model
            var model = this.MembershipRepository.GetAddGroupMemberViewModel(id);

            //
            return View(model);
        }

        /// <summary>
        /// Add group member POST
        /// </summary>
        /// <param name="model"></param>
        /// <param name="id"></param>
        /// <returns></returns>
        [ProggyBackEndAuthorize(MembershipConstants.MEMBERSHIP_MANAGER_GROUP_NAME)]
        [HttpPost]
        public ActionResult AddGroupMembers(AddGroupMembersViewModel model, int id)
        {
            // 
            var errorMsg = "";

            //
            if (this.MembershipRepository.GetAddMembersOk(model, MembershipContext.Current.CurrentMember.Username, ref errorMsg))
            {
                return RedirectToAction("EditGroup", new { id = model.Group.GroupId, success = 8 });
            }


            //
            ModelState.AddModelError("", errorMsg);
            return View(model);
        }

        /// <summary>
        /// Remove group member and redirect back to group
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [ProggyBackEndAuthorize(MembershipConstants.MEMBERSHIP_MANAGER_GROUP_NAME)]
        [HttpGet]
        public ActionResult RemoveGroupMember(int id, int groupId)
        {
            // remove and get group
            this.MembershipRepository.RemoveGroupMember(id);

            //
            return RedirectToAction("EditGroup", new { id = groupId, success = 4 });
        }

        /// <summary>
        /// New group GET
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        [ProggyBackEndAuthorize(MembershipConstants.MEMBERSHIP_MANAGER_GROUP_NAME)]
        public ActionResult GroupNew()
        {
            return View();
        }

        /// <summary>
        /// New group POST
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        [HttpPost]
        [ProggyBackEndAuthorize(MembershipConstants.MEMBERSHIP_MANAGER_GROUP_NAME)]
        public ActionResult GroupNew(Proggy.Data.Entities.Group model)
        {
            // start error
            var errorMsg = string.Empty;

            // check
            if (ModelState.IsValid)
            {
                // check again
                if (this.MembershipRepository.GetAddGroupOk(model.GroupName, MembershipContext.Current.CurrentMember.Username, ref errorMsg)) {
                    return RedirectToAction("Groups", new { success = 14 });
                }
            }

            // error
            ModelState.AddModelError("", errorMsg);
            return View(model);
        }
    }
}
